<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  login.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-19.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Login extends CI_Controller {
	function __construct()
	{
		parent::__construct();
 		$this->load->model('common/security_model');
		if($this->security_model->isLogin() === FALSE){
			showmessage('请登录', 'index', 2);
		}
		$this->load->model('user_model');
		$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
		$this->md5_username = substr(md5($this->user['username']), 0, 24);
		$this->md5_password = substr(md5($this->user['password']), 0, 24);
		if (!checkperm('manageconfig')) {
			showmessage('您没有权限登陆此页面', 'index', 2);
		}
	}
	
	function index(){
		$data = array();
		$data['userinfo'] = $this->user;
		$password = $this->input->post('password') ? md5(md5($this->input->post('password', true)).$this->user['salt']) : '';
		// session未超时 自动登录
		if ($this->session->userdata('adminuserinfo')) {
			showmessage('自动登录成功', 'admin/index', 1);
		}
		
		if (submitcheck('loginsubmit')) {
			$loginuser = $this->user_model->getuser('uid', $this->user['uid']);
			if ($password == $loginuser['password']) {
				$userinfo = array(
					'username' => $this->md5_username,
					'password' => $this->md5_password,
				);
				$this->session->set_userdata('adminuserinfo', $userinfo);
				showmessage('登录成功', 'admin/index', 3);
			}{
				showmessage('密码错误', 'admin/login', 3);
			}
		}
		$this->load->template('admin/login_tpl', $data);
	}
	
	function logout($username){
		$this->session->sess_destroy();
		showmessage('退出成功', 'admin/login/index', 3);	
	}
}
?>